﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Charting;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.ChartView;
using LeoK.DCI.Entity;
using LeoK.DCI.Bll;
using LeoK.Common;

namespace LeoK.DCI.Views
{
    /// <summary>
    /// DCIEditor.xaml 的交互逻辑
    /// </summary>
    public partial class DCIEditor : RadWindow
    {
        private DCI_DayLine _curDayLine;

        public DCIEditor()
        {
            InitializeComponent();

            datePicker.SelectedDate = DateTime.Now;

            DataContext = this;
            Unloaded += (o, e) => SaveData();
        }

        public async void LoadData(DateTime date)
        {
            try
            {
                Header = date.ToString("yyyy/MM/dd");

                cbkLocked.IsChecked = true;

                _curDayLine = await DCIManager.GetDayLine(date);
                double openValue = _curDayLine.PreClose;

                VAxis.Maximum = openValue * 1.11;
                VAxis.Minimum = openValue * 0.89;
                VAxis.MajorStep = _curDayLine.Open * 0.2 * 0.05;
                VAxis.LabelInterval = 5;

                LineSeries.ItemsSource = null;
                LineSeries.ItemsSource = _curDayLine.DayPoints;
                slineMA.ItemsSource = DCIManager.GetMA(_curDayLine.DayPoints);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }

        }

        private void SaveData()
        {
            try
            {
                DCIManager.Save(_curDayLine);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
        }

        private void LineSeries_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LineSeries.BeginInit();
            try
            {
                if (!cbkLocked.IsChecked.Value)
                {
                    DataTuple d = RadCartesianChart.ConvertPointToData(e.GetPosition(RadCartesianChart), HAxis, VAxis);

                    if (d.SecondValue != null)
                    {
                        foreach (DayPoint dayPoint in _curDayLine.DayPoints.Where(p => p.Time >= (DateTime) d.FirstValue))
                        {
                            dayPoint.Value = (double) d.SecondValue;
                        }

                        slineMA.ItemsSource = DCIManager.GetMA(_curDayLine.DayPoints);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
            finally
            {
                LineSeries.EndInit();
            }
        }


        private void btnPrev_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveData();
                datePicker.SelectedValue = _curDayLine.Date.AddDays(-1);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
        }

        private void btnNext_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveData();
                datePicker.SelectedValue = _curDayLine.Date.AddDays(1);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
        }

        private void DatePicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                LoadData(datePicker.SelectedValue.Value);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
        }

        private void RadCartesianChart_OnMouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                DataTuple d = RadCartesianChart.ConvertPointToData(e.GetPosition(RadCartesianChart), HAxis, VAxis);

                if (d != null && d.FirstValue != null && d.SecondValue != null)
                {
                    VAGridLineAnnotation.Value = d.SecondValue;
                    VAGridLineAnnotation.Label = (Math.Round((double) d.SecondValue, 2)).ToString();
                    HAGridLineAnnotation.Value = d.FirstValue;
                    HAGridLineAnnotation.Label = ((DateTime) d.FirstValue).ToString("HH:mm");
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex);
            }
        }
    }

}
